<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="dataForm" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<u-form-item label="采购单别" v-if="judgeShow('tm001')">
				<u-input v-model="infoObj.tm001" placeholder="请输入采购单别" disabled></u-input>
			</u-form-item>
			<u-form-item label="采购单号" v-if="judgeShow('tm002')">
				<u-input v-model="infoObj.tm002" placeholder="请输入采购单号" disabled></u-input>
			</u-form-item>
			<u-form-item label="单据日期" v-if="judgeShow('tm003')">
				<u-input v-model="infoObj.tm003" placeholder="请输入单据日期" disabled></u-input>
			</u-form-item>
			<u-form-item label="供应商" v-if="judgeShow('tm005')">
				<u-input v-model="buyName" placeholder="请输入供应商" disabled></u-input>
			</u-form-item>
			<u-form-item label="采购人员" v-if="judgeShow('tm012')">
				<u-input v-model="buyNameTwo" placeholder="采购人员" disabled></u-input>
			</u-form-item>
			<u-form-item label="交易币种" v-if="judgeShow('tm006')">
				<u-input v-model="infoObj.tm006" placeholder="请输入交易币种" disabled></u-input>
			</u-form-item>
			<u-form-item label="汇率" v-if="judgeShow('tm007')">
				<u-input v-model="infoObj.tm007" placeholder="请输入汇率" disabled></u-input>
			</u-form-item>
			<u-form-item label="付款条件" v-if="judgeShow('tm009')">
				<u-input v-model="buyNameThree" placeholder="请输入付款条件" disabled></u-input>
			</u-form-item>

			<u-form-item label="税率" v-if="judgeShow('tm017')">
				<u-input v-model="infoObj.tm017" placeholder="请输入税率" disabled></u-input>
			</u-form-item>

			<u-form-item label="采购金额" v-if="judgeShow('tm018')">
				<u-input v-model="infoObj.tm018" placeholder="请输入采购金额" disabled></u-input>
			</u-form-item>
			<u-form-item label="税额" v-if="judgeShow('tm019')">
				<u-input v-model="infoObj.tm019" placeholder="请输入税额" disabled></u-input>
			</u-form-item>

			<u-form-item label="价税合计">
				<u-input v-model="infoObj.tm019+infoObj.tm018" placeholder="请输入价税合计" disabled></u-input>
			</u-form-item>

			<u-form-item label="送货地址" v-if="judgeShow('tm020')">
				<u-input v-model="infoObj.tm020" placeholder="请输入送货地址" disabled></u-input>
			</u-form-item>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in lst" :key="i">
					<u-form-item label="序号" prop="orderCode">
						{{i+1}}
					</u-form-item>
					<u-form-item label="品号" prop="orderCode" v-if="judgeShow('tn004')">
						<u-input v-model="item.tn004" placeholder="品号" disabled></u-input>
					</u-form-item>
					<u-form-item label="规格" prop="orderCode" v-if="judgeShow('tn005')">
						<u-input v-model="item.tn005" placeholder="规格" disabled></u-input>
					</u-form-item>
					<u-form-item label="采购数量" prop="orderCode" v-if="judgeShow('tn006')">
						<u-input v-model="item.tn006" placeholder="材料规格" disabled></u-input>
					</u-form-item>
					<u-form-item label="采购单价" prop="orderCode" v-if="judgeShow('tn008')">
						<u-input v-model="item.tn008" placeholder="采购单价" disabled></u-input>
					</u-form-item>
					<u-form-item label="采购金额" prop="orderCode" v-if="judgeShow('tn009')">
						<u-input v-model="item.tn009" placeholder="采购金额" disabled></u-input>
					</u-form-item>
					<u-form-item label="已交数量" prop="orderCode" v-if="judgeShow('tn011')">
						<u-input v-model="item.tn011" placeholder="已交数量" disabled></u-input>
					</u-form-item>
					<u-form-item label="单位" prop="orderCode" v-if="judgeShow('tn007')">
						<u-input v-model="item.tn007" placeholder="单位" disabled></u-input>
					</u-form-item>
					<u-form-item label="预交货日" prop="orderCode" v-if="judgeShow('tn015')">
						<u-input v-model="item.tn015" placeholder="单位" disabled></u-input>
					</u-form-item>
					<u-form-item label="备注" prop="orderCode" v-if="judgeShow('tn010')">
						<u-input v-model="item.tn010" type="textarea" placeholder="备注" disabled></u-input>
					</u-form-item>
				</view>
			</view>
		</u-form>
	</view>
</template>

<script>
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update,
	} from '@/api/apply/order'
	import {
		getAsttmInfo
	} from '@/api/workFlow/flowBefore'
	import {
		getDictionaryDataSelector,
		getDataInterfaceDataInfo
	} from '@/api/common'
	export default {
		mixins: [comMixin],
		data() {
			return {
				buyName: '',
				buyNameTwo: '',
				buyNameThree:'',
				categoriesOfTaxesName: '',
				categoriesOfTaxesLst: [],
				tel: '', //联系人
				telTwo: '', //联系人
				hide: false,
				billEnCode: 'OrderNumber',
				dataForm: {},
				infoObj: {},
				lst: [],
				dictionaryInfo: {},
				rules: {
					customerId: [{
						required: true,
						message: '客户名称不能为空',
						trigger: 'input'
					}],
					salesmanId: [{
						required: true,
						message: '业务人员不能为空',
						trigger: 'input'
					}],
					orderDate: [{
						required: true,
						message: '订单日期不能为空',
						trigger: 'change',
						type: 'number'
					}],
					paymentMode: [{
						required: true,
						message: '付款方式不能为空',
						trigger: 'input'
					}]
				},
				options: [{
					id: '现金',
					fullName: '现金'
				}, {
					id: '转帐',
					fullName: '转帐'
				}, {
					id: '汇票',
					fullName: '汇票'
				}],
				info: {},
				formConf: {},
				listInfo: {
					optionOne: '',
					optionTwo: '',
					optionThree: '',
					optionFour: '',
					JoinStr: '',
					gochang: '',
				},
				id: '',
			}
		},
		methods: {
			init(data, id) {
				this.setting = data
				this.id = id
				this.getInfoFn(id)

			},
			getInfoFn(data) {
				getAsttmInfo(data).then(res => {
					if (res.code == 200) {
						this.lst = res.data.asttnList
						this.infoObj = res.data

						this.getDataInterfaceDataInfoMV002(this.infoObj.tm005)
						this.getDataInterfaceDataInfoMA003(this.infoObj.tm012)
						this.getDataInterfaceDataInfoMA004(this.infoObj.tm009)
					}
				})
			},
			/**
			 * 查询税种列表
			 */
			getDictionaryDataSelectorFn() {
				getDictionaryDataSelector('411695330707391429').then(res => {
					if (res.code == 200) {
						this.categoriesOfTaxesLst = res.data.list
						this.categoriesOfTaxesLst.forEach(res => {
							if (res.enCode == this.infoObj.tc018) {
								this.categoriesOfTaxesName = res.fullName
							}
						})
					}
				})
			},

			getDataInterfaceDataInfoMV002(id) {
				let data = {
					id,
					interfaceId: '435309912533253253',
					dblink: this.id,
					propsValue: 'MA001',
					relationField: 'JoinStr',
				}
			 
				getDataInterfaceDataInfo('435309912533253253', data).then(res => {
					if (res.code == 200) {
						this.buyName = res.data.JoinStr
					 
					}
				})
			},
			getDataInterfaceDataInfoMA003(id) {
				let data = {
					id,
					interfaceId: '428345732257285957',
					dblink: this.id,
					propsValue: 'MV001',
					relationField: 'JoinStr',
				}
				 
				getDataInterfaceDataInfo('428345732257285957', data).then(res => {
					if (res.code == 200) {
						this.buyNameTwo = res.data.JoinStr
					}
				})
			},
			getDataInterfaceDataInfoMA004(id) {
				let data = {
					id,
					interfaceId: '362115953258928709',
					dblink: this.id,
					propsValue: 'NA002',
					relationField: 'JoinStr',
				}

				getDataInterfaceDataInfo('362115953258928709', data).then(res => {
					if (res.code == 200) {
						this.buyNameThree = res.data.JoinStr
					}
				})
			},
			getDataInterfaceDataInfoJoinStr(id) {
				let data = {
					id,
					interfaceId: 362115953258928709,
					propsValue: 'NA002',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('362115953258928709', data).then(res => {
					if (res.code == 200) {
						this.listInfo.JoinStr = res.data.JoinStr
					}
				})
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>